package com.dap.system.office.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.dap.common.core.exception.CustomException;
import com.dap.common.core.utils.DateUtils;
import com.dap.common.core.utils.SecurityUtils;
import com.dap.common.core.utils.StringUtils;
import com.dap.system.office.domain.TCourse;
import com.dap.system.office.domain.vo.*;
import com.dap.system.office.mapper.TCourseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import com.dap.system.office.mapper.TScoreMapper;
import com.dap.system.office.domain.TScore;
import com.dap.system.office.service.ITScoreService;

import javax.annotation.Resource;

/**
 * 分数Service业务层处理
 *
 * @author dap
 * @date 2021-11-12
 */
@Service
public class TScoreServiceImpl implements ITScoreService {
    @Resource
    private TScoreMapper tScoreMapper;
    @Resource
    private TCourseMapper tCourseMapper;

    /**
     * 查询分数
     *
     * @param id 分数ID
     * @return 分数
     */
    @Override
    public TScore selectTScoreById(Long id) {
        return tScoreMapper.selectTScoreById(id);
    }

    /**
     * 查询分数列表
     *
     * @param tScore 分数
     * @return 分数
     */
    @Override
    public List<TScore> selectTScoreList(TScore tScore) {
        return tScoreMapper.selectTScoreList(tScore);
    }

    /**
     * 分页查询分数列表
     *
     * @param tScore 分数
     * @return 分数
     */
    @Override
    public List<TScore> selectTScoreListPage(TScore tScore) {
        PageHelper.startPage(tScore.getPageNum(), tScore.getPageSize());
        List<TScore> list = tScoreMapper.selectTScoreList(tScore);
        PageInfo<TScore> tPageInfo = new PageInfo<>(list);
        List<TScore> dataList = tPageInfo.getList();
        return dataList;
    }

    /**
     * 新增分数
     *
     * @param tScore 分数
     * @return 结果
     */
    @Override
    public int insertTScore(TScore tScore) {
        TCourse course = new TCourse();
        course.setId(course.getId());
        List<TCourse> tCourses = tCourseMapper.selectTCourseList(course);
        if (tCourses.size() > 0) {
            throw new CustomException("该课程的分数已存在");
        }
        tScore.setCreateTime(DateUtils.getNowDate());
        tScore.setDelFlag("0");
        tScore.setCreateBy(SecurityUtils.getUsername());
        return tScoreMapper.insertTScore(tScore);
    }

    /**
     * 修改分数
     *
     * @param tScore 分数
     * @return 结果
     */
    @Override
    public int updateTScore(TScore tScore) {
        TCourse course = new TCourse();
        course.setId(course.getId());
        List<TCourse> tCourses = tCourseMapper.selectTCourseList(course);
        if (tCourses.size() > 0) {
            throw new CustomException("该课程的分数已存在,禁止修改");
        }
        tScore.setUpdateTime(DateUtils.getNowDate());
        return tScoreMapper.updateTScore(tScore);
    }

    /**
     * 批量删除分数
     *
     * @param ids 需要删除的分数ID
     * @return 结果
     */
    @Override
    public int deleteTScoreByIds(Long[] ids) {
        return tScoreMapper.deleteTScoreByIds(ids);
    }

    /**
     * 删除分数信息
     *
     * @param id 分数ID
     * @return 结果
     */
    @Override
    public int deleteTScoreById(Long id) {
        return tScoreMapper.deleteTScoreById(id);
    }

    /**
     * 根据课程得分数得到平均分的等级
     *
     * @param tScore
     * @return
     */
    @Override
    public List<TStudentScoreInfoVo> selectStudentScoreInfo(TScore tScore) {
        List<TStudentScoreInfoVo> tStudentScoreInfoVos = tScoreMapper.selectStudentScoreInfo(tScore);
        // 根据平均分计算等级
        return tStudentScoreInfoVos.stream().map(this::resultScoreGrade).collect(Collectors.toList());
    }


    private TStudentScoreInfoVo resultScoreGrade(TStudentScoreInfoVo tStudentScoreInfoVo){
        Double avgScore = tStudentScoreInfoVo.getAvgScore();
        if(avgScore == null || avgScore <= 0 || avgScore >= 100 ){
            tStudentScoreInfoVo.setGrade(0);
            return tStudentScoreInfoVo;
        }

        if(avgScore >= 90){
            tStudentScoreInfoVo.setGrade(4);
            return tStudentScoreInfoVo;
        }

        if(avgScore >= 80 && avgScore < 90){
            tStudentScoreInfoVo.setGrade(3);
            return tStudentScoreInfoVo;
        }

        if(avgScore >= 60&& avgScore < 80){
            tStudentScoreInfoVo.setGrade(2);
            return tStudentScoreInfoVo;
        }

        tStudentScoreInfoVo.setGrade(1);
        return tStudentScoreInfoVo;


    }


    //导入数据
    @Override
    public String importScore(List<TScore> scoreList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(scoreList) || scoreList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }else{
            for (TScore scores : scoreList){
                TScore score=tScoreMapper.selectTScore(scores.getId());
            }

            return "导入成功";
        }
    }


}
